﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

namespace ConsoleApplication
{
    /// <summary>
    /// ReadFileNames 的摘要说明。
    /// C#获取文件夹中所有文件的文件名.
    /// 想实现这样一个功能：批量修改一个目录所有jpg文件的文件名，在其原始基础上加上特定字符串
    ///我先把一个目录里所有的格式为jpg的文件名存储到数组里，然后统一修改数组值来批量修改jpg文件的文件名
    ///现在想知道怎么获取文件夹中所有后缀名为jpg文件的文件名 ]
    ///分数不多如果解决问题加一百分
    ///现在文件名已经获取了，怎么才能写回到每个文件中呢
    /// </summary>
    class ReadFiles
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        public static void ReadFileNames()
        {
            string dirp = @"E:\Files\works\MyCodeShop\my-codeshop\MyWebSite\Data\File\Images";
            DirectoryInfo mydir = new DirectoryInfo(dirp);
            foreach (FileSystemInfo fsi in mydir.GetFileSystemInfos())
            {
                if (fsi is FileInfo)
                {
                    FileInfo fi = (FileInfo)fsi;
                    string x = System.IO.Path.GetDirectoryName(fi.FullName);
                    Console.WriteLine(x);
                    string s = System.IO.Path.GetExtension(fi.FullName);
                    string y = System.IO.Path.GetFileNameWithoutExtension(fi.FullName);
                    Console.WriteLine(y);
                    if (s == ".png")
                    {
                        //System.IO.File.Copy(fi.FullName, x + @"\oo" + fi.Name); //在原文件名前加上OO
                        //System.IO.File.Copy(fi.FullName, x + @"\" + fi.Name.Replace(@"oo", "")); //在原文件名前减去OO
                        //System.IO.File.Copy(fi.FullName, x + @"\" + fi.Name.Replace("png", "jpg"));
                        //System.IO.File.Copy(fi.FullName, x + @"\" + fi.Name.Replace("jpg", "png"));
                        //System.IO.File.Delete(fi.FullName);

                        if (y == "pucca")   //  用 garu.png 的内容替换 pucca.png 的内容，但文件名不变; 同时备份原 pucca.png 为 123.png
                        { System.IO.File.Replace(fi.FullName.Replace("pucca", "garu"), fi.FullName, x + @"\" + "123.png"); }
                    }
                }

            }
            Console.WriteLine("成功");
            Console.ReadLine();
        }

        public static void ReadFileContentTest()
        {
            string dirp = @"E:\Files\works\MyCodeShop\my-codeshop\MyWebSite\Data\File\txt";

            try
            {
                DirectoryInfo mydir = new DirectoryInfo(dirp);
                foreach (FileSystemInfo fsi in mydir.GetFileSystemInfos())
                {
                    FileInfo fi = (FileInfo)fsi;
                    string path = System.IO.Path.GetDirectoryName(fi.FullName);
                    string ext = System.IO.Path.GetExtension(fi.FullName);
                    string name = System.IO.Path.GetFileNameWithoutExtension(fi.FullName);

                    FileStream fs = new FileStream(fi.FullName, FileMode.OpenOrCreate);
                    //Console.WriteLine(fs.Name);
                    //Console.ReadKey();

                    StreamReader sd = new StreamReader(fs);
                    StringBuilder sb = new StringBuilder();
                    string line;
                    string value;
                    //sb.Append(sd.ReadLine() + "\n");
                    //sb.Append(sd.ReadLine() + "\n");
                    //sb.Append(sd.ReadLine() + "\n");
                    //sb.Append(sd.ReadToEnd());
                    //while ((line = sd.ReadLine()) != null)    //  可以是空行
                    //while ((line = sd.ReadLine()) != String.Empty)    //  不可以是空行
                    //while ((line = sd.ReadLine()).Contains("iParameter"))
                    while ((line = sd.ReadLine()) != null)
                    {
                        if (line.IndexOf("iParameter2", 0) >= 0)
                        {
                            value = line.Substring(line.IndexOf("=", 0) + 1).Trim();
                            Console.WriteLine(line);
                            Console.WriteLine(value);
                            continue;
                        }
                    }
                    Console.ReadKey();
                    //Console.WriteLine(sb.ToString());
                    //Console.ReadKey();
                    sd.Close();

                }
            }
            catch (IOException ex)
            { string Err = ex.Message;}
        }

        public static void ReadFileContent()
        {
            Console.WriteLine(getCfgParamValue(AppConfigFiles.MyText, "strparameter1"));
            Console.ReadKey();

            //  string.Join()
            string[] str = { "A", "BC", "D", "EFG" };
            string strnew = string.Join(";", str);
            Console.WriteLine(strnew);
            Console.ReadKey();
        }

        public static string getCfgParamValue(AppConfigFiles ACF, string ParameterName)
        {
            string returnValue = string.Empty;
            string FilePath;
            switch (ACF)
            { 
                case AppConfigFiles.MyText:
                case AppConfigFiles.AppConfig:
                case AppConfigFiles.Others:
                default:
                    //FilePath = @"E:\Files\works\MyCodeShop\my-codeshop\MyWebSite\Data\File\txt\MyText.txt"; break;
                    FilePath = @"E:\Files\works\MyCodeShop\my-codeshop\MyWebSite\Data\File\txt\MyText2.ini"; break;
            }

            try
            {
                using (FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Read))
                {
                    StreamReader sd = new StreamReader(fs);
                    string line;
                    while ((line = sd.ReadLine()) != null)
                    {
                        if (line.ToLower().IndexOf(ParameterName.ToLower(), 0) >= 0)
                        {
                            returnValue = line.Substring(line.IndexOf("=", 0) + 1).Trim();
                            break;
                        }
                    }
                    //sd.Close();
                }
            }
            catch (Exception ex)
            { returnValue = ex.Message; }

            return returnValue;
        }


    }
}
